package houseRent.Service;

/*
1.定义house数组，保存house对象
2。响应houseview的调用
3.完成对各种房屋信息的操作处理
 */

import com.sun.org.apache.regexp.internal.RE;
import houseRent.Domain.House;

public class HouseService {
    private House[] houses;//保存house对象
    private int houseCount=1;
    private int newID=0;//可以看这里是后台 可以将我们的数据进行分配给我们的用户使用
    public HouseService(int size) {
        houses=new House[size];//进行数组大小的初始化
        houses[0]=new House(0,"lisi","111","山东",2000,"我i出");
    }
    //书写list方法，返回houses
    public House[] List(){
        return houses;
    }
    public boolean add(House newhouse){
        if(houseCount==houses.length){
            System.out.println("添加信息失败");
            return  false;
        }
        else{
            houses[houseCount++]=newhouse;//添加数据到了数据库里面
            newhouse.setId(++newID);//设置ID成功
            return true;
        }

    }
    public boolean delete(int deleteID){
        int index=-1;
        //查找有没有出现过这个元素
        for(int i=0;i<houseCount;i++){
            if(houses[i].getId()==deleteID){
                index=i;//这个记录一下 也是为了一会方便删除
            }

        }
        if(index==-1){
            return false;
        }
        //这里实现具体的删除
        int i=0;
        for( i=index;i<houseCount-1;i++){
            houses[i]=houses[i+1];
        }
        houses[i]=null;//最后一个元素设置为空
        return true;


    }
    public boolean read(int ID){
        for(int i=0;i<houseCount;i++){
            if(houses[i].getId()==ID){
                System.out.println(houses[i].toString());
                return true;
            }
        }
        return false;
    }
    public boolean find(int ID){
        for(int i=0;i<houseCount;i++){
            if(houses[i].getId()==ID){
                return true;
            }
        }
        return false;
    }
    public boolean Modify(House newhouse,int newID){
        int index=-1;
        for(int i=0;i<houseCount;i++){
            if(houses[i].getId()==newID){
                index=i;
                break;
            }
        }
        if(index==-1){
            return false;
        }
        else{
            houses[index]=newhouse;//将从前端拿到的数据 赋值给新的数据
            return true;
        }

    }

}
